# -*- coding: iso-8859-2 -*-

from authorshipanalyzer.slot.SlotFiller import SlotFiller
from authorshipanalyzer.slot import Slot
from authorshipanalyzer.utils import getKey

class SlotManager:
    '''
    Keep single message slots
    '''
    
    def __init__(self):
        self.slots = []
        self.slotFiller = SlotFiller()
        
        
    def createSlots(self, messages, authors = {}):
        '''
        Create and add slots of given messages
        @messages: list of all messages
        @authors: dictionary <author, message_number>; omit if empty
        '''
            
        for message in messages:
            if len(authors) == 0:
                print "|",
                self.createSlot(message)
            else:
                auth = message.getAuthor()
                key = getKey(authors, auth)
                if key != None and authors[key] > 0:
                    authors[key] -= 1
                    print "|",
                    self.createSlot(message)
            
    def createSlot(self, message):
        '''
        Create and add slot of given message
        '''
        self.slots.append(self.slotFiller.createSlot(message))
        
    def getSlots(self):
        return self.slots
        
if __name__ == "__main__":
    import datetime
    import os
    
    from authorshipanalyzer.message import Message
    from authorshipanalyzer.message import MessageReader
    from authorshipanalyzer.file import Path
    from authorshipanalyzer.textmodifier import MessagePreprocessor
    from authorshipanalyzer.slot import Slot

    fromDate = datetime.date(2000, 5, 1)
    toDate = datetime.date(2000, 5, 3)
    
    mr = MessageReader(Path.getMainDir() + os.sep + "info1")
    messages = mr.readMessages(fromDate, toDate)
#    messages = MessagePreprocessor().preprocess(messages)
    
    from time import time
    
    start = time()
    sm = SlotManager()
    sm.createSlots(messages)
    end = time()
    
    print "Creating slots: " + str(end - start) + " seconds"
    print "Number of messages:", str(len(messages))
    
    for slot in sm.getSlots():
         print "Author:", slot.getAuthor()
         print "Sex:", slot.getSex()
         print "Use diacritics:", slot.getUseDiacritics()
         print "Category:", slot.getCategory()
         print "Subject:", slot.getSubject()
         print "Text:", slot.getMessage().getText()
         print "-------------\n\n" 
    
#    ca = CategoryFiller()
#    
#    for message in messages:
#        print "Author: " + message.getAuthor()
#        print "Subject: " + message.getSubject()
#        print "Addess: " + message.getAddress()
#        print "Text:\n" + message.getText()     
#        print "---------->", ca.analyze(message)
#        print "\n\n"